package com.qianfeng.smartdevices.controller;


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevices.annotations.AopLogAnnotation;
import com.qianfeng.smartdevices.dto.R;
import com.qianfeng.smartdevices.pojo.Device;
import com.qianfeng.smartdevices.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/devices")
public class DeviceController {

    private DeviceService deviceService;
    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }
    @GetMapping("/devices")
    @AopLogAnnotation("设备分页查询")
    public R findAllDevice(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit, String address, String status,@RequestParam(defaultValue = "-100") Long categoryid,@RequestParam(defaultValue = "-100")Long areaid){
        PageInfo<Device> allDevice = deviceService.findAllDevice(page, limit, address, status,categoryid,areaid);

        return R.setOK(allDevice);
    }
    @PostMapping("/device")
    @AopLogAnnotation("设备添加")
    public R addDevice(@RequestBody Device device)throws Exception{
        deviceService.addDevice(device);
        return R.setOK();
    }
    @PutMapping("/device")
    @AopLogAnnotation("设备更新")
    public R updateDevice(@RequestBody Device device) throws Exception {
        deviceService.updateDevice(device);
        return R.setOK();
    }
    @DeleteMapping("/devices")
    @AopLogAnnotation("设备删除")
    public R deleteDeviceById(String[] ids){
        deviceService.deleteDeviceById(ids);
        return R.setOK();
    }
    @PostMapping("/command/{uuid}/{command}")
    public void sendCommand(@PathVariable String uuid,@PathVariable String command) {
        deviceService.sendCommand(uuid,command);
    }
}
